﻿<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>书籍列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/icon.css}">
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript">
        //工具栏
        var toolbar = [{
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                let selected = $('#booklist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择待修改的书籍");
                } else {
                    $("#bookid").val(selected.id);
                    $("#bookname").textbox("setValue", selected.bookname);
                    $("#price").textbox("setValue", selected.price);
                    $("#groupprice").textbox("setValue", selected.groupprice);
                    $("#bookintroduce").textbox("setValue", selected.bookintroduce);
                    $("#authorintroduce").textbox("setValue", selected.authorintroduce);
                    $("#image").attr("src","/manager"+selected.imgurl);
                    $('#dd').dialog('open');
                }
            }
        }, '-', {
            text: '启用',
            iconCls: 'icon-ok',
            handler: function () {
                let selected = $('#booklist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择要修改状态的用户");
                } else {
                    $.ajax({
                        url: "/manager/book/status/1",
                        data: {
                            "id": selected.id
                        },
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            if (data.status !== 2001) {
                                alert(data.msg);
                            } else {
                                $('#booklist').datagrid('reload');
                            }
                        }
                    });
                }
            }
        }, '-', {
            text: '禁用',
            iconCls: 'icon-cancel',
            handler: function () {
                let selected = $('#booklist').datagrid('getSelected');
                if (!selected) {
                    alert("请选择要修改状态的用户");
                } else {
                    $.ajax({
                        url: "/manager/book/status/-1",
                        data: {
                            "id": selected.id
                        },
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            if (data.status !== 2001) {
                                alert(data.msg);
                            } else {
                                $('#booklist').datagrid('reload');
                            }
                        }
                    });
                }
            }
        }];
        $(function () {
            $("#booknameKey").val("");
            $("#ISBNKey").val("");
            $("#press").val("");
            $("#supplier").val("");
            $('#booklist').datagrid({
                title: '书籍列表',
                iconCls: 'icon-save',
                url:"/manager/book/findAll",
                columns: [
                    [
                        {field: 'bookname', title: '名称', width: 100, align: 'center'},
                        {field: 'author', title: '作者', width: 200, align: 'center'},
                        {field: 'price', title: '单价', width: 80, align: 'center'},
                        {field: 'id', hidden: true},
                        {field: 'groupprice', hidden: true},
                        {field: 'bookintroduce', hidden: true},
                        {field: 'authorintroduce', hidden: true},
                        {field: 'imgurl', hidden: true},
                        {field: 'iSBN', title: 'ISBN', width: 200, align: 'center'},
                        {field: 'press', title: '出版社', width: 200, align: 'center',
                        formatter:function (value, row, index) {
                            if (value == null){
                                return "--";
                            }
                            return value.name;
                        }},
                        {field: 'suppliers', title: '供货商', width: 500, align: 'center',
                            formatter:function (value, row, index) {
                                if (value == null||value.length === 0){
                                    return "--"
                                }else {
                                    let result = "";
                                    for (let valueElement of value) {
                                        result =result+","+ valueElement.name;
                                    }
                                    return result.substring(1);
                                }
                            }
                        },
                        {field: 'flag', title: '状态', width: 100, align: 'center',
                            formatter: function (value, row, index) {
                                if (value === 1) {
                                    return "上架";
                                }else if (value === -1){
                                    return "下架";
                                }else if (value === 2){
                                    return "缺货";
                                }
                                return "审核中";
                            }
                        },
                    ]
                ],
                toolbar: toolbar,
                pagination: true,//表示在datagrid设置分页
                pageSize: 5,
                singleSelect: true,
                pageList: [5, 10, 20]
            });

            //初始化查询栏
            $("#submit_search").linkbutton({iconCls: 'icon-search', plain: true})
                .click(function () {
                    $('#booklist').datagrid({
                        queryParams:
                            {
                                "booknameKey": $("#booknameKey").val(),
                                "ISBNKey": $("#ISBNKey").val(),
                                "pressId":$("#press").combobox('getValue'),
                                "supplierId":$("#supplier").combobox('getValue')
                            }
                    });   //点击搜索
                });

            //初始化修改窗口
            $('#dd').dialog({
                title: '书籍信息修改',
                width: 700,
                height: 500,
                closed: true,
                cache: false,
                href: '',
                modal: true,
                iconCls: 'icon-save',
                buttons: [{
                    text: '提交',
                    iconCls: 'icon-ok',
                    handler: function () {
                        $("#bookModify").form({
                            url: "book/modifyBook",
                            success: function (data) {
                                $('#dd').dialog('close');
                                alert(JSON.parse(data).msg);
                                $('#booklist').datagrid('reload');
                            }
                        })
                        $("#bookModify").submit();
                    }
                }, {
                    text: '关闭',
                    handler: function () {
                        $('#dd').dialog('close');
                    }
                }]
            });

            //初始化下拉控件
            $("#press").combobox({
                url: "/manager/press/findAllNoPages",
                prompt:'请选择出版社',
                value:"",
                valueField: "id",
                textField: "name"
            });


            $("#supplier").combobox({
                url: "/manager/supplier/findAllNoPages",
                prompt:'请选择供应商',
                value:"",
                valueField: "id",
                textField: "name"
            });

        });
    </script>
</head>
<body>
<form name="searchform" method="post" action="" id="searchform">
    <td width="70" height="30"><strong>书籍查询：</strong></td>
    <td height="30">
        <input type="text" name="booknameKey" id="booknameKey" size=20 placeholder="书籍名称"/>
        <input type="text" name="ISBNKey" id="ISBNKey" size=20 placeholder="ISBN"/>
        <select name="press" id="press" style="width:200px">
        </select>
        <select name="supplier" id="supplier" style="width:200px">
        </select>
    </td>
    <td>
        <a id="submit_search">搜索</a>
    </td>
</form>
<table id="booklist"></table>
<div id="dd">
    <div align="center" style="margin-top: 30px">
        <form id="bookModify" method="post" enctype="multipart/form-data">
            <input type="hidden" name="bookid" id="bookid">
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="bookname" id="bookname" style="width:50%"
                       data-options="label:'书籍名称:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="price" id="price" style="width:50%"
                       data-options="label:'单价:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="groupprice" id="groupprice"style="width:50%"
                       data-options="label:'团购价:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="bookintroduce" id="bookintroduce" style="width:50%;height:60px"
                       data-options="label:'书籍简介:',multiline:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="authorintroduce" id="authorintroduce" style="width:50%;height:60px"
                       data-options="label:'作者简介:',multiline:true">
            </div>
            <div style="margin-bottom:20px">
                封面图片:&nbsp;&nbsp;&nbsp;&nbsp; <input type="file" id="file" name="file"/>
            </div>
            <div style="margin-bottom:20px">
                当前封面：&nbsp;&nbsp;&nbsp;&nbsp; <img id="image" src="#" style="width:200px">
            </div>
        </form>
    </div>
</div>
</body>
</html>